package com.x.xiaoshuo.widget.readview.aim;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Region;
import android.graphics.drawable.GradientDrawable;

import com.x.xiaoshuo.manager.SettingManager;
import com.x.xiaoshuo.manager.ThemeManager;


public class SlideAim implements AimInterface {
    private Path mPath0;

    GradientDrawable mBackShadowDrawableLR;
    GradientDrawable mBackShadowDrawableRL;
    ReadView readView;

    public SlideAim(ReadView readView) {

        readView.mTouch.x = 0.01f;
        readView.mTouch.y = 0.01f;

        mPath0 = new Path();

        int[] mBackShadowColors = new int[]{0xaa666666, 0x666666};
        mBackShadowDrawableRL = new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, mBackShadowColors);
        mBackShadowDrawableRL.setGradientType(GradientDrawable.LINEAR_GRADIENT);

        mBackShadowDrawableLR = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, mBackShadowColors);
        mBackShadowDrawableLR.setGradientType(GradientDrawable.LINEAR_GRADIENT);

        this.readView = readView;
    }

    @Override
    public void drawCurrentPageArea(Canvas canvas) {
        mPath0.reset();

        canvas.save();
        if (readView.actiondownX > readView.mScreenWidth >> 1) {
            mPath0.moveTo(readView.mScreenWidth + readView.touch_down, 0);
            mPath0.lineTo(readView.mScreenWidth + readView.touch_down, readView.mScreenHeight);
            mPath0.lineTo(readView.mScreenWidth, readView.mScreenHeight);
            mPath0.lineTo(readView.mScreenWidth, 0);
            mPath0.lineTo(readView.mScreenWidth + readView.touch_down, 0);
            mPath0.close();
            canvas.clipPath(mPath0, Region.Op.XOR);
            canvas.drawBitmap(readView.mCurPageBitmap, readView.touch_down, 0, null);
        } else {
            mPath0.moveTo(readView.touch_down, 0);
            mPath0.lineTo(readView.touch_down, readView.mScreenHeight);
            mPath0.lineTo(readView.mScreenWidth, readView.mScreenHeight);
            mPath0.lineTo(readView.mScreenWidth, 0);
            mPath0.lineTo(readView.touch_down, 0);
            mPath0.close();
            canvas.clipPath(mPath0);
            canvas.drawBitmap(readView.mCurPageBitmap, readView.touch_down, 0, null);
        }
        try {
            canvas.restore();
        } catch (Exception e) {

        }
    }

    @Override
    public void drawCurrentPageShadow(Canvas canvas) {

    }

    @Override
    public void drawCurrentBackArea(Canvas canvas) {
        // none
    }

    @Override
    public void drawNextPageAreaAndShadow(Canvas canvas) {
        canvas.save();
        if (readView.actiondownX > readView.mScreenWidth >> 1) {
            canvas.clipPath(mPath0);
            canvas.translate(readView.mScreenWidth + readView.touch_down, 0);
            canvas.drawBitmap(readView.mNextPageBitmap, 0, 0, null);
        } else {
            canvas.clipPath(mPath0, Region.Op.XOR);
            canvas.translate(0 - readView.mScreenWidth + readView.touch_down, 0);
            canvas.drawBitmap(readView.mNextPageBitmap, 0, 0, null);
        }
        try {
            canvas.restore();
        } catch (Exception e) {

        }
    }

    @Override
    public void calcPoints() {

    }

    @Override
    public void calcCornerXY(float x, float y) {

    }

    @Override
    public void computeScroll() {
        if (readView.mScroller.computeScrollOffset()) {
            float x = readView.mScroller.getCurrX();
            float y = readView.mScroller.getCurrY();
            if (readView.actiondownX > readView.mScreenWidth >> 1) {
                readView.touch_down = -(readView.mScreenWidth - x);
            } else {
                readView.touch_down = x;
            }
            readView.mTouch.y = y;
            //touch_down = mTouch.x - actiondownX;
            readView.postInvalidate();
        }
    }

    @Override
    public void jumpToChapter(int chapter) {

    }

    @Override
    public void destory() {

    }

    @Override
    public void startAnimation() {
        int dx;
        if (readView.actiondownX > readView.mScreenWidth / 2) {
            dx = (int) -(readView.mScreenWidth + readView.touch_down);
            readView.mScroller.startScroll((int) (readView.mScreenWidth + readView.touch_down), (int) readView.mTouch.y, dx, 0, 700);
        } else {
            dx = (int) (readView.mScreenWidth - readView.touch_down);
            readView.mScroller.startScroll((int) readView.touch_down, (int) readView.mTouch.y, dx, 0, 700);
        }
    }

    @Override
    public void abortAnimation() {
        if (!readView.mScroller.isFinished()) {
            readView.mScroller.abortAnimation();
        }
    }

    @Override
    public void restoreAnimation() {
        int dx;
        if (readView.actiondownX > readView.mScreenWidth / 2) {
            dx = (int) (readView.mScreenWidth - readView.mTouch.x);
        } else {
            dx = (int) (-readView.mTouch.x);
        }
        readView.mScroller.startScroll((int) readView.mTouch.x, (int) readView.mTouch.y, dx, 0, 300);
    }

    public void setBitmaps(Bitmap bm1, Bitmap bm2) {
        readView.mCurPageBitmap = bm1;
        readView.mNextPageBitmap = bm2;
    }

    @Override
    public synchronized void setTheme(int theme) {
        readView.resetTouchPoint();
        Bitmap bg = ThemeManager.getThemeDrawable(theme);
        if (bg != null) {
            readView.pagefactory.setBgBitmap(bg);
            if (readView.isPrepared) {
                readView.pagefactory.onDraw(readView.mCurrentPageCanvas);
                readView.pagefactory.onDraw(readView.mNextPageCanvas);
                readView.postInvalidate();
            }
        }
        if (theme < 5) {
            SettingManager.getInstance().saveReadTheme(theme);
        }
    }
}
